<?php
/**
 *
 * @author yupoxiong<i@yupoxiong.com>
 */

declare (strict_types=1);

namespace app\admin\traits;

use support\Response;

trait AdminEditTrait
{
    /**
     * 修改
     */
    public function edit(): Response
    {
        $model    = $this->model;
        $validate = $this->validate;

        $request  = request();
        $id       = ($request)->input('id');

        $data = $model->findOrEmpty($id);
        if (($request)->method() === 'POST') {
            $param = ($request)->all();

            $check = $validate->scene('admin_edit')->check($param);
            if (!$check) {
                return admin_error($validate->getError());
            }

            $result = $data->save($param);

            return $result ? admin_success('修改成功', [], URL_BACK) : admin_error('修改失败');
        }

        $this->assign([
            'data' => $data,
        ]);

        return $this->fetch('add');
    }

}